package cn.pigeon.health.service.impl;

import cn.pigeon.health.constant.RedisKeyConstant;
import cn.pigeon.health.dao.SetMealDao;
import cn.pigeon.health.entity.PageResult;
import cn.pigeon.health.entity.QueryPageBean;
import cn.pigeon.health.pojo.Setmeal;
import cn.pigeon.health.service.SetMealService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 套餐服务实现类
 */
@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService{
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 套餐分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

        Page<Setmeal> page = setMealDao.findByPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 添加套餐
     * @param setmeal
     * @param checkGroupIds
     */
    public void add(Setmeal setmeal, Integer[] checkGroupIds) {
        //添加基本信息
        setMealDao.add(setmeal);
        //添加检查组关联信息
        addSetmealAndCheckGroups(setmeal.getId(),checkGroupIds);
        //把图片名称保存到Redis
        jedisPool.getResource().sadd(RedisKeyConstant.SETMEAL_UPLOAD_PIC_DB,setmeal.getImg());
    }

    /**
     * 查询套餐关联的检查组
     * @param id
     * @return
     */
    public List<Integer> findCheckGroupsBySetMealId(Integer id) {
        return setMealDao.findCheckGroupsBySetMealId(id);
    }

    /**
     * 根据ID查找基本套餐信息
     * @param id
     * @return
     */
    public Setmeal findById(Integer id) {
        return setMealDao.findById(id);
    }

    /**
     * 编辑套餐
     * @param setmeal
     * @param checkGroupIds
     */
    public void edit(Setmeal setmeal, Integer[] checkGroupIds) {
        Integer setmealId = setmeal.getId();
        //保存基本信息
        setMealDao.edit(setmeal);
        //删除原有套餐的关联检查组信息
        setMealDao.deleteSetMealCheckGroups(setmealId);
        //保存新的套餐的关联检查组信息
        addSetmealAndCheckGroups(setmeal.getId(),checkGroupIds);

        //查找老的套餐图片信息
        Setmeal setmealQuery = setMealDao.findById(setmealId);
        jedisPool.getResource().srem(RedisKeyConstant.SETMEAL_UPLOAD_PIC_DB,setmealQuery.getImg());
        //把图片名称保存到Redis
        jedisPool.getResource().sadd(RedisKeyConstant.SETMEAL_UPLOAD_PIC_DB,setmeal.getImg());
    }

    /**
     * 根据ID删除套餐
     * @param id
     */
    public void delete(Integer id) {
        //删除原有套餐的关联检查组信息
        setMealDao.deleteSetMealCheckGroups(id);
        //删除套餐
        setMealDao.delete(id);
    }

    /**
     * 获取套餐列表
     * @return
     */
    public List<Setmeal> getAll() {
        List<Setmeal> lstData = setMealDao.getAll();
        return lstData;
    }

    /**
     * 根据Id查询套餐详情
     * @param id
     * @return
     */
    public Setmeal findByIdForDetail(Integer id) {
        return setMealDao.findByIdForDetail(id);
    }

    /**
     * 添加套餐和检查组的关联信息
     * @param setmealId
     * @param CheckGroupIds
     */
    private void addSetmealAndCheckGroups(Integer setmealId,Integer[] CheckGroupIds) {
        for (Integer checkGroupId : CheckGroupIds) {
            Map<String,Integer> map = new HashMap<String,Integer>();
            map.put("setMealId",setmealId);
            map.put("checkGroupId",checkGroupId);
            setMealDao.addSetmealAndCheckGroups(map);
        }
    }
}
